package com.ujiuye.service.impl;

import com.ujiuye.mapper.OrderListMapper;
import com.ujiuye.service.IOrderListService;
import com.ujiuye.utils.DateUtils;
import com.ujiuye.utils.PageResult;
import com.ujiuye.utils.QueryPageBean;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class OrderListServiceImpl implements IOrderListService {

    @Resource
    private OrderListMapper orderListMapper;

    @Override
    public PageResult listOrder(QueryPageBean queryPageBean) {
        String queryString = queryPageBean.getQueryString();
        List<Map<String, Object>> list = null;
        //判断查询条件是否为空
        if (queryString!=null && queryString.length()>0){
            //执行查询
            list = orderListMapper.listOrderByPhoneNumber(queryString,0,queryPageBean.getPageSize());
        }else {
            //执行查询
            list = orderListMapper.listOrderByPhoneNumber(null,queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
//            list = orderListMapper.listOrder(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        }
        //查询所有总数
        Long total = orderListMapper.count();
        //创建返回结果对象
        return new PageResult(total,list);
    }

    @Override
    public PageResult listOrderByWeek(QueryPageBean queryPageBean) {
        Date monday = DateUtils.getThisWeekMonday();
        Date sunday = DateUtils.getSundayOfThisWeek();
        List<Map<String, Object>> list = orderListMapper.listOrderByWeekOrMonth(monday,sunday,queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //查询所有总数
        Long total = orderListMapper.count();
        //创建返回结果对象
        return new PageResult(total,list);
    }

    @Override
    public PageResult listOrderByMonth(QueryPageBean queryPageBean) {
        Date month = DateUtils.getFirstDay4ThisMonth();
        String monthString = new SimpleDateFormat("yyyy-MM").format(month);
        String endMonth = monthString+"-31";
        try {
            Date lastDate = new SimpleDateFormat("yyyy-MM-dd").parse(endMonth);
            List<Map<String, Object>> list = orderListMapper.listOrderByWeekOrMonth(month,lastDate,queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
            //查询所有总数
            Long total = orderListMapper.count();
            //创建返回结果对象
            return new PageResult(total,list);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }

    @Override
    public boolean updateOrderByOrderId(int orderId) {
        return orderListMapper.updateOrderByOrderId(orderId)>0;
    }

}
